package com.resource.authoritymanage.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.excel.util.StringUtils;
import com.resource.authoritymanage.entity.TIdentifyAuditEntity;
import com.resource.authoritymanage.service.TIdentifyApplyService;
import com.resource.authoritymanage.service.TIdentifyAuditService;
import com.resource.authoritymanage.service.TIdentifyRecordService;
import com.resource.common.utils.AppResponse;
import com.resource.common.utils.PageUtils;
import com.resource.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 用户权限审批表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-03-30 11:07:34
 */
@RestController
@RequestMapping("/authority/tidentifyaudit")
public class TIdentifyAuditController {

    @Autowired
    private TIdentifyAuditService tIdentifyAuditService;

    @Autowired
    private TIdentifyApplyService tIdentifyApplyService;

    @Autowired
    private TIdentifyRecordService tIdentifyRecordService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tIdentifyAuditService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		TIdentifyAuditEntity tIdentifyAudit = tIdentifyAuditService.getById(id);
        return R.ok().put("tIdentifyAudit", tIdentifyAudit);
    }

    /**
     * 保存
     */
    @RequestMapping("/audit")
    public R save(@RequestBody TIdentifyAuditEntity tIdentifyAudit){
       String auditResult= tIdentifyAudit.getAuditResult();
//        String auditRemark = tIdentifyAudit.getAuditRemark();
        String applyNo = tIdentifyAudit.getApplyNo();
        if(!StringUtils.isEmpty(auditResult) && "false".equals(auditResult)){
            tIdentifyAudit.setAuditStatus("已审批");
            tIdentifyAudit.setAuditResult("不通过");
            tIdentifyAuditService.updateById(tIdentifyAudit);
        }else if(!StringUtils.isEmpty(auditResult) && "true".equals(auditResult)){
            tIdentifyAudit.setAuditStatus("已审批");
            tIdentifyAudit.setAuditResult("通过");
            tIdentifyAuditService.updateById(tIdentifyAudit);
            tIdentifyApplyService.updateByAuditResult(auditResult,applyNo);
            tIdentifyRecordService.saveRecord(tIdentifyAudit,applyNo);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TIdentifyAuditEntity tIdentifyAudit){
		tIdentifyAuditService.updateById(tIdentifyAudit);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		tIdentifyAuditService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
